package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.MemberMapper;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service(interfaceClass = MemberService.class)
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public Member findByTelephone(String telephone) {

        return memberMapper.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {

        memberMapper.add(member);

    }

    @Override
    public Map getMemberReport() throws Exception {
        //获取一个日历对象
        Calendar calendar = Calendar.getInstance();
        //将日期往前推12个月
        calendar.add(Calendar.MONTH, -12);
        //定义一个集合，用来存储每个月的会员数量
        List<Integer> memberCounts = new ArrayList();
        //定义一个集合，用来存储过去的12个月
        List<String> memberMonths = new ArrayList();
        for (int i = 0; i < 12; i++) {
            Date time = calendar.getTime();
            String date = DateUtils.parseDate2String(time, "yyyy-MM");
            memberMonths.add(date);
            //定义每个月的查询的起始值
            String dateBegin = date + "-01";
            //定义每个月的查询的结束值
            String dateEnd = date + "-31";
            //查询每个月的会员数量
            int count = memberMapper.findMemberCountByMonth(dateBegin, dateEnd);
            memberCounts.add(count);
            System.out.println(date);
            calendar.add(Calendar.MONTH, 1);
        }
        Map map = new HashMap();
        map.put("memberMonths", memberMonths);
        map.put("memberCounts", memberCounts);
        return map;
    }

    @Override
    public Map getMemberReportByDays(String start, String end) throws Exception {
        Date date01 = DateUtils.parseString2Date(start, "yyyy-MM-dd");
        Date date02 = DateUtils.parseString2Date(end, "yyyy-MM-dd");
        //定义一个集合，用来存储每个月的会员数量
        List<Integer> memberCounts = new ArrayList();
        //定义一个集合，用来存储月数
        List<String> memberMonths = new ArrayList();
        while (date01.getTime() <= date02.getTime()) {
            String mouth = DateUtils.parseDate2String(date01, "yyyy-MM");
            memberMonths.add(mouth);
            //定义每个月的查询的起始值
            String dateBegin = mouth + "-01";
            //定义每个月的查询的结束值
            String dateEnd = mouth + "-31";
            //查询每个月的会员数量
            int count = memberMapper.findMemberCountByMonth(dateBegin, dateEnd);
            memberCounts.add(count);
            //加一个月
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date01);
            calendar.add(Calendar.MONTH, 1);
            Date date = calendar.getTime();
            date01 = date;
        }
        Map map = new HashMap();
        map.put("memberMonths", memberMonths);
        map.put("memberCounts", memberCounts);
        return map;
    }

    @Override
    public Map getMemberReportByAge() {
        List<String> ageList = new ArrayList<>();
        List<Map<String, String>> valueMapList = new ArrayList<>();
        ageList.add("0-17");
        ageList.add("18-30");
        ageList.add("31-60");
        ageList.add("61-");
        int size = ageList.size();
        for (int i = 0; i < size; i++) {
            HashMap<String, String> temp = new HashMap<>();
            String str = ageList.get(i);
            String[] split = str.split("-");
            temp.put("start", split[0]);
            if (split.length == 2) temp.put("end", split[1]);
            int count = memberMapper.findCountByAge(temp);
            if (split.length < 2) str = str.replace("-", "以上");
            ageList.set(i, str);
            temp.clear();
            temp.put("name", str);
            temp.put("value", count + "");
            valueMapList.add(temp);
        }
        Map<String, Object> resultData = new HashMap<String, Object>(2);
        resultData.put("names", ageList);
        resultData.put("counts", valueMapList);
        return resultData;
    }

    @Override
    public Map getMemberReportBySex() {
        List<Map<String, String>> valueMapList = new ArrayList<>();
        List<Map> counts = memberMapper.getCountsBySex();
        List<String> names = new ArrayList<>(3);
        for (Map count : counts) {
            if (count.get("sex").equals("1")) {
                count.put("sex", "女");
            } else if (count.get("sex").equals("2")) {
                count.put("sex", "男");
            }
            names.add((String) count.get("sex"));
            Map<String, String> map = new HashMap<>();
            map.put("name", (String) count.get("sex"));
            map.put("value", count.get("num")+"");
            valueMapList.add(map);
        }
        Map<String, Object> resultData = new HashMap<String, Object>(2);
        resultData.put("names", names);
        resultData.put("counts", valueMapList);
        return resultData;
    }

   /* public static void main(String[] args) throws Exception {
        //获取一个日历对象
        Calendar calendar = Calendar.getInstance();
        //将日期往前推12个月
        calendar.add(Calendar.MONTH,-12);

        for (int i = 0; i < 12; i++) {
            Date time = calendar.getTime();

            String date = DateUtils.parseDate2String(time,"yyyy-MM");

            //定义每个月的查询的起始值
            String dateBegin = date+"-01";
            //定义每个月的查询的结束值
            String dateEnd = date+"-31";

            System.out.println(date);
            calendar.add(Calendar.MONTH,1);

        }


    }*/
}
